package com.nulldev.util.web.nTube;

import com.nulldev.util.data.Variables;
import com.nulldev.util.io.file.FileUtil;
import com.nulldev.util.web.HttpServer3.Page;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.extensions.fakeFlare.CDNCGITracePage;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3.CachingMethod;
import com.nulldev.util.web.nTube.components.video_queue.VideoQueueThread;
import com.nulldev.util.web.nTube.pages.AboutAPIPage;
import com.nulldev.util.web.nTube.pages.Base64DecodingPage;
import com.nulldev.util.web.nTube.pages.ChannelAPIPage;
import com.nulldev.util.web.nTube.pages.DASHTranslatorPage;
import com.nulldev.util.web.nTube.pages.FallbackWatchPage;
import com.nulldev.util.web.nTube.pages.FlashWatchPage;
import com.nulldev.util.web.nTube.pages.GetCommentsPage;
import com.nulldev.util.web.nTube.pages.GetPagePage;
import com.nulldev.util.web.nTube.pages.GetSponsorsPage;
import com.nulldev.util.web.nTube.pages.GetTrendingPage;
import com.nulldev.util.web.nTube.pages.GetVideoAsyncPage;
import com.nulldev.util.web.nTube.pages.JSCSSGeneratorPage;
import com.nulldev.util.web.nTube.pages.ProxyAPIAsyncPage;
import com.nulldev.util.web.nTube.pages.SRTGeneratorPage;
import com.nulldev.util.web.nTube.pages.SearchVideoAPIPage;
import com.nulldev.util.web.nTube.pages.WarningIconPage;
import com.nulldev.util.web.nTube.pages.WebVTTGeneratorPage;
import com.nulldev.util.web.nTube.pages.misc.APIInstanceInfoPage;
import com.nulldev.util.web.nTube.pages.misc.InstancePresentCheckPage;
import com.nulldev.util.web.nTube.pages.misc.NetworkThroughputTestPage;
import com.nulldev.util.web.nTube.pages.misc.ProxyGetDislikesPage;
import com.nulldev.util.web.nTube.pages.nmusic.NMusicGetPage;
import com.nulldev.util.web.nTube.pages.ntv.NTVGetMetaPage;
import com.nulldev.util.web.nTube.pages.ntv.NTVGetPage;
import com.nulldev.util.web.nTube.pages.streams.ClientVideoRequestPage;
import com.nulldev.util.web.nTube.pages.streams.ClientVideoRequestStream;
import com.nulldev.util.web.nTubeExtensions.yt_chat_on_ntube.YTChatPolymerInjector;
import com.nulldev.util.web.nTubeExtensions.yt_chat_on_ntube.YTChooseServerProxifier;
import com.nulldev.util.web.nTubeExtensions.yt_chat_on_ntube.YTLiveChatProxifier;
import com.nulldev.util.web.nTubeExtensions.yt_chat_on_ntube.YTServerSignalProxifier;
import com.nulldev.util.web.nTubeExtensions.yte_for_archives.impl.NTubeArchiveRedirectorPage;

final class NTubeAPILoader {

	private final NTubeServer nst;
	private final VideoQueueThread VIDEO_QUEUE;

	private final HttpPage3 PROXY_YT_DRM;
	private final HttpPage3 PROXY_YT_CHAT;

	public NTubeAPILoader(final NTubeServer inst, final VideoQueueThread VIDEO_QUEUE, final HttpPage3 PROXY_YT_DRM, final HttpPage3 PROXY_YT_CHAT) {
		this.nst = Variables.requireNonNullObject(inst, "inst");
		this.VIDEO_QUEUE = Variables.requireNonNullObject(VIDEO_QUEUE, "VIDEO_QUEUE");
		this.PROXY_YT_DRM = Variables.requireNonNullObject(PROXY_YT_DRM, "PROXY_YT_DRM");
		this.PROXY_YT_CHAT = Variables.requireNonNullObject(PROXY_YT_CHAT, "PROXY_YT_CHAT");
	}

	/* */

	private boolean addPage(final Page page) {
		return this.nst.addPage(page);
	}

	/* */

	public void loadDefaults() {
		/* Pages */
		addPage(new WarningIconPage());
		addPage(new CDNCGITracePage()); // Use fakeFlare.
		addPage(new AboutAPIPage(nst));
		addPage(new APIInstanceInfoPage(nst));
		addPage(new ChannelAPIPage());
		// &bc is required for bitchute videos
		addPage(new GetVideoAsyncPage(VIDEO_QUEUE));
		addPage(new SearchVideoAPIPage());
		addPage(new NMusicGetPage());
		addPage(new NTVGetPage());
		addPage(new NTVGetMetaPage());
		addPage(new ProxyAPIAsyncPage().setCaptchaExclusion(true));
		addPage(new FallbackWatchPage());
		addPage(new FlashWatchPage());
		addPage(new Base64DecodingPage().setCaptchaExclusion(true));
		addPage(new WebVTTGeneratorPage());
		addPage(new SRTGeneratorPage().setCaptchaExclusion(true));
		addPage(new JSCSSGeneratorPage());
		addPage(new GetCommentsPage());
		addPage(new GetTrendingPage());
		addPage(new GetSponsorsPage());
		addPage(new InstancePresentCheckPage());
		addPage(new ProxyGetDislikesPage());
		addPage(new GetPagePage());
		addPage(new DASHTranslatorPage());
		addPage(new NetworkThroughputTestPage());
		addPage(PROXY_YT_DRM);
		addPage(PROXY_YT_CHAT);
		addPage(new HttpPage3("/nsearch.xml") {

			@SuppressWarnings("resource")
			@Override
			public Response onRequest(Request request) {
				return new Response(200, FileUtil.getResourceStream(NTubeServer.class, "nsearch.xml")).setCustomMime("application/opensearchdescription+xml");
			}
		}.setCachingMethod(CachingMethod.SHORT_CACHE));
		final ClientVideoRequestStream cvrs = new ClientVideoRequestStream();
		addPage(cvrs);
		addPage(new ClientVideoRequestPage(cvrs, VIDEO_QUEUE));
		/* nTube Archive Viewer */
		addPage(new NTubeArchiveRedirectorPage());
		/* yt_chat on nTube */
		addPage(new YTLiveChatProxifier());
		addPage(new YTChatPolymerInjector());
		addPage(new YTChooseServerProxifier());
		addPage(new YTServerSignalProxifier());
		/* */
	}

	public void loadEmbeddedOnly() {
		/* Pages */
		addPage(new WarningIconPage());
		addPage(new CDNCGITracePage()); // Use fakeFlare.
		addPage(new AboutAPIPage(nst));
		addPage(new APIInstanceInfoPage(nst));
		/* */
		addPage(new GetVideoAsyncPage(VIDEO_QUEUE));
		addPage(new GetSponsorsPage());
		/* */
		addPage(new ProxyAPIAsyncPage().setCaptchaExclusion(true));
		addPage(new Base64DecodingPage().setCaptchaExclusion(true));
		addPage(new WebVTTGeneratorPage());
	}

	public void loadInvidiousOnly() {
		/* Pages */
		addPage(new WarningIconPage());
		addPage(new CDNCGITracePage()); // Use fakeFlare.
		addPage(new AboutAPIPage(nst));
		addPage(new APIInstanceInfoPage(nst));
		/* */
		addPage(new ProxyAPIAsyncPage().setCaptchaExclusion(true));
		addPage(new Base64DecodingPage().setCaptchaExclusion(true));
		addPage(new WebVTTGeneratorPage());
	}
}
